package aidn.animation.controller 
{
	import aidn.animation.events.CustomEvent;
	import aidn.animation.text.TextBitmap;
	import aidn.animation.utils.Utils;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	public class GridBoxManager 
	{
		private var _target :DisplayObjectContainer;
		
		private var _boxSize   :int;
		private var _blockSize :int;
		
		private var _num     :int;
		private var _margin  :int;
		
		private var _tts     :/*Number*/Array;
		private var _txList  :/*String*/Array;
		
		private var _time    :Number;		// 持続時間 (単位:秒)
		private var _font    :String;
		
		private var _timeManager :TimeManager;
		
		
		private var _col     :uint = 0;
		private var _colHira :uint = 0;
		private var _colKata :uint = 0;
		
		public function GridBoxManager() 
		{
			
		}
		public function init ( target :DisplayObjectContainer, boxSize :int = 400, num :int = 9, margin :int = 1 ) :void
		{
			_target  = target;
			
			if (_target is Sprite)
			{
				var g :Graphics = Sprite(_target).graphics;
				g.beginFill(0xffffff, 0);
				// g.lineStyle(0, 0xbbbbbb);
				g.drawRect( -boxSize/2, -boxSize/2, boxSize, boxSize);
			}
			
			_boxSize = boxSize;
			_num     = num;
			_margin  = margin;
			
			_blockSize = _boxSize / (num + 1);
			
			trace("block size:", _blockSize);
		}
		
		public function setParam ( tts :/*Number*/Array, txList :/*String*/Array, time :Number = 3, font :String = null ) :void
		{
			if (! _target) trace(this, "not init");
			
			trace("   tts len:", tts.length);
			trace("txList len:", txList.length);
			
			_tts    = tts;
			_txList = txList;
			
			_time   = time;
			_font   = font;
			
			_timeManager = new TimeManager(_target);
			_timeManager.addEventListener(CustomEvent.UPDATE, _update);
			_timeManager.setTts(_tts);
		}
		
		public function setColor ( col :uint, colHira :uint, colKata :uint ) :void
		{
			_col     = col;
			_colHira = colHira;
			_colKata = colKata;
		}
		
		public function start ( ) :void
		{
			if (! _target) trace(this, "not init");
			if (! _tts)    trace(this, "not setParam");
			
			_timeManager.start();
		}
		public function stop ( ) :void
		{
			_timeManager.stop();
		}
		
		
		/* UPDATE */
		private function _update ( evt :CustomEvent ) :void 
		{
			var n :int = evt.position;
			
			
			var txbmp :TextBitmap = new TextBitmap();
			
			     if (Utils.checkHiragana(_txList[n]))	txbmp.init(_txList[n], _font, _colHira, 96);
			else if (Utils.checkKatakana(_txList[n]) || _txList[n] == "ー")	txbmp.init(_txList[n], _font, _colKata, 96);
			else										txbmp.init(_txList[n], _font, _col,     96);
			
			// width, height
			
			//txbmp.width = txbmp.height = _blockSize - _margin * 2;	// タテヨコおなじ
			//*
			if (txbmp.width < txbmp.height)
			{
				txbmp.height = _blockSize - _margin * 2;
				txbmp.scaleX = txbmp.scaleY;
			}
			else
			{
				txbmp.width  = _blockSize - _margin * 2;
				txbmp.scaleY = txbmp.scaleX;
			}
			// */
			// ---
			
			// x, y, rotation
			var a :int = int(n / _num) % 4;		// 0 - 3
			var b :int = n % _num;
			
			var px :int, py :int, rot :int;
			
			switch ( a )
			{
			case 0:	rot = 0;	px = ( -_boxSize + _blockSize) / 2 + b * _blockSize;	py = ( -_boxSize + _blockSize) / 2;						break;
			case 1:	rot = 90;	px = (  _boxSize - _blockSize) / 2;						py = ( -_boxSize + _blockSize) / 2 + b * _blockSize;	break;
			case 2:	rot = 180;	px = (  _boxSize - _blockSize) / 2 - b * _blockSize;	py = (  _boxSize - _blockSize) / 2;						break;
			case 3:	rot = 270;	px = ( -_boxSize + _blockSize) / 2;						py = (  _boxSize - _blockSize) / 2 - b * _blockSize;	break;
			}
			
			txbmp.rotation = rot;
			txbmp.x = px;
			txbmp.y = py;
			// ---
			
			// 表示 & 非表示
			txbmp.show(Utils.randInt(0, 3), _time);
			_target.addChild(txbmp);
			
		}
	}
}